﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Product;
using Broker.View.Comm;
using BrokerBase.Lib.Core;
using Telerik.Windows.Controls;
using BrokerBase.Lib.Tool;

namespace Broker.View.ProductView
{
    /// <summary>
    /// RelationForm.xaml 的交互逻辑
    /// </summary>
    public partial class RelationForm 
    {
        private IRelation relation;
        private IRelation original;
        private IProduct owner;

        public RelationForm(IRelation relation,IProduct owner)
        {
            original = relation;

            this.relation = new Relation();
            this.owner = owner;

            BaseTools.CopyTo(original, this.relation);
            InitializeComponent();
            Loaded += new RoutedEventHandler(RelationForm_Loaded);
            PreviewClosed += new EventHandler<Telerik.Windows.Controls.WindowPreviewClosedEventArgs>(RelationForm_PreviewClosed);
        }

        

        private void CreatePeriodConstraintModel()
        {
            IList<PeriodConstraintModel> models = new List<PeriodConstraintModel>();

            models.Add(new PeriodConstraintModel("小于等于",PeriodConstraintType.LessOrEqual));
            models.Add(new PeriodConstraintModel("等于", PeriodConstraintType.Equal));
            models.Add(new PeriodConstraintModel("无限制", PeriodConstraintType.None));

            cmbPeriodConstraint.ItemsSource = models;
        }

        public bool IsCommit { get; set; }

        public Action<IRelation> CallBack;

        void RelationForm_PreviewClosed(object sender, Telerik.Windows.Controls.WindowPreviewClosedEventArgs e)
        {
            if (IsCommit)
            {
                if (Verify())
                {
                    BaseTools.CopyTo<IRelation>(relation, original);
                    if (CallBack != null)
                    {
                        CallBack(original);
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        void RelationForm_Loaded(object sender, RoutedEventArgs e)
        {
            CreatePeriodConstraintModel();
            DataContext = relation;

            
            SetViewControl();
            
        }

        private void SetViewControl()
        {
            bool isBasic = owner.ProductType == ProductType.Basic;

            txtBasicName.IsReadOnly = isBasic;
            txtExtraName.IsReadOnly = !isBasic;

            btnSelBasicProduct.Visibility = isBasic ? Visibility.Hidden : Visibility.Visible;
            btnSelExtraProduct.Visibility = isBasic ? Visibility.Visible : Visibility.Hidden;

            txtBasicName.IsReadOnly = owner.IsCheck;
            txtExtraName.IsReadOnly = owner.IsCheck;

            cmbPeriodConstraint.IsEnabled = !owner.IsCheck;
            chkIsTerminationEqualBasic.IsEnabled = !owner.IsCheck;
            chkIsShareAmount.IsEnabled = !owner.IsCheck;
            chkIsForceRelation.IsEnabled = !owner.IsCheck;

            btnSelBasicProduct.IsEnabled = !owner.IsCheck;
            btnSelExtraProduct.IsEnabled = !owner.IsCheck;
        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = true;
            Close();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = false;
            Close();
        }

        private bool Verify()
        {
            if( relation.Basic == null)
            {
                MessageBox.Show("请选择关联的主险","提示");
                return false;
            }

            if (relation.Extra == null)
            {
                MessageBox.Show("请选择关联的附险", "提示");
                return false;
            }

            return true;
        }

        private void btnSelBasicProduct_Click(object sender, RoutedEventArgs e)
        {
            IBaseService<BasicProduct> svr = new BaseService<BasicProduct>(LibGlobal.HbmSessionFactory.GetCurrentSession());

            IList<IBasicProduct> ls = svr.GetListByHQL(string.Format("from BasicProduct as p where  p.Company.Id={0} Order by p.Name", relation.Extra.Company.Id)).Cast<IBasicProduct>().ToList<IBasicProduct>();

            ListSelForm frm = new ListSelForm();
            frm.Owner = this;
            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            GridViewDataColumn col1 = new GridViewDataColumn();
            col1.Header = "名称";
            col1.Width = new GridViewLength(200, GridViewLengthUnitType.Star);
            col1.DataMemberBinding = new Binding("Name");

            frm.gvList.Columns.Add(col1);

            //IList<IBasicProduct> bps = frm.MultiSelecteByList<IBasicProduct>(ls);

            IBasicProduct bp = frm.SelectByList<IBasicProduct>(ls);

            if (bp == null)
                return;

            relation.Basic = bp;

            txtBasicName.Text = bp.Name;
        }

        private void btnSelExtraProduct_Click(object sender, RoutedEventArgs e)
        {
            IBaseService<ExtraProduct> svr = new BaseService<ExtraProduct>(LibGlobal.HbmSessionFactory.GetCurrentSession());

            IList<IExtraProduct> ls = svr.GetListByHQL(string.Format("from ExtraProduct as p where  p.Company.Id={0} Order by p.Name", relation.Basic.Id)).Cast<IExtraProduct>().ToList<IExtraProduct>();

            ListSelForm frm = new ListSelForm();
            frm.Owner = this;
            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            GridViewDataColumn col1 = new GridViewDataColumn();
            col1.Header = "名称";
            col1.Width = new GridViewLength(200, GridViewLengthUnitType.Star);
            col1.DataMemberBinding = new Binding("Name");

            frm.gvList.Columns.Add(col1);

            IExtraProduct ep = frm.SelectByList<IExtraProduct>(ls);

            if (ep == null)
                return;

            relation.Extra = ep;
            txtExtraName.Text = ep.Name;
        }
    }

    class PeriodConstraintModel
    {
        public PeriodConstraintModel(string name, PeriodConstraintType value)
        {
            Name = name;
            Value = value;
        }

        public string Name { get; set; }

        public PeriodConstraintType Value { get; set; }
    }
}
